<template>
	<view class="app-pages setting">

		<view class="title">信息填写</view>
		<view class="cell">
			<u--form :model="form" ref="uForm" :rules="rules" labelWidth="60px">
				<u-form-item label="姓 名:" prop="val">
					<u-input v-model="form.val" />
				</u-form-item>

				<u-form-item label="手机号:" prop="val">
					<u-input v-model="form.val" />
				</u-form-item>

				<u-form-item label="卡 号:" prop="val">
					<u-input v-model="form.val" />
				</u-form-item>

				<u-form-item label="工 号:" prop="val">
					<u-input v-model="form.val" />
				</u-form-item>
			</u--form>
		</view>

		<view class="title">信息验证</view>
		<view class="cell">
			<view class="cell-item">
				<view class="cell-item-title">工卡正面</view>


				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" maxCount="1"
					:maxCount="1" width="250" height="150">
					<image src="https://cdn.uviewui.com/uview/demo/upload/positive.png" mode="widthFix"
						style="width: 250px;height: 150px;"></image>
				</u-upload>
			</view>
			<view class="cell-item">
				<view class="cell-item-title">工卡反面</view>
				<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" maxCount="1"
 					:maxCount="1" width="250" height="150">
					<image src="https://cdn.uviewui.com/uview/demo/upload/positive.png" mode="widthFix"
						style="width: 250px;height: 150px;"></image>
				</u-upload>
			</view>
		</view>

		<view class="button">
			<view class="submit" @click="submit">提交</view>
		</view>
	</view>
</template>
<script>
	export default {
		name: 'Setting',
		data() {
			return {
				fileList1: [],
				fileList2: [],
				form: {
					val: ''
				},
				rules: {
					val: {
						required: true,
						message: '请输入内容',
						trigger: ['blur', 'change']
					}
				},
				value: '18733842561'
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			submit() {
				this.$refs.uForm.validate().then(res => {
					uni.$u.toast('校验通过')
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			}
		},
	}
</script>
<style lang="scss" scoped>
	.setting {
		.button {
			box-sizing: border-box;
			padding-top: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		
			.submit {
				width: 100%;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				background-color: #f74062;
				border-radius: 10rpx;
				color: #FFFFFF;
			}
		}
		.title {
			font-size: 32rpx;
			font-weight: bold;
			margin: 30rpx 0;
		}

		.cell {
			padding-left: 60rpx;

			.cell-item {
				margin-top: 20rpx;

				.cell-item-title {
					margin-bottom: 15rpx;
				}
			}
		}
	}
</style>
